package com.mgface.service;

import com.mgface.dao.URLDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;
/**
 * @author wanyuxiang
 * @version 1.0
 * @project spring-boot-oauth2-system
 * @create 2019-11-20 17:07
 **/
@Repository
public class URLService {

    @Autowired
    URLDao urlDao;

    @Autowired
    RedisService redisService;

    //验证权限
    public boolean checkURL(String accessToken, String url) {
        String client_id = redisService.get("access_token_" + accessToken);

        if (StringUtils.isEmpty(client_id)) {
            return false;
        }

        url = url.replaceAll("/[0-9][0-9]?", "/#");

        List<String> URLs = urlDao.getURLs(client_id);
        for (String str : URLs) {
            str = str.replaceAll("\\{[^{}]*}", "#");

            if (str.equals(url)) {
                return true;
            }
        }
        return false;
    }
}
